<template>
    <div v-loading="loading">
        <CommonPage :state="state" btnType="save" :flowId="infoData.flowId" @submitForm="valid">
            <Info style="padding: 20px" ref="form" v-model="infoData" :state="state"></Info>
        </CommonPage>


        <!--        <div v-if="'write'==state" class="card-wrap flex justify-end card-padding">-->
        <!--            <el-button @click="cancel">取消</el-button>-->
        <!--            <el-button-->
        <!--                type="primary"-->
        <!--                @click="valid">提交</el-button>-->
        <!--        </div>-->

    </div>
</template>

<script>
import Info from "./info.vue";
import {carWorkOrderInfo, gpsRenew, saveCarGpsRenew} from "@/api/gps/gpsList.js";
import {isEmpty} from "lodash";


export default {
    name: "GPSRenewal",
    components: {Info},
    dicts: [],
    data() {
        return {
            loading: false,
            infoData: {},
            state: 'write'
        };
    },
    async mounted() {
        this.state = this.$route?.query?.type
        const title =  this.$route?.query?.pageType??'GPS续费工单'
        const route = Object.assign({}, this.$route, { title: `${title}` })
        this.$store.dispatch('tagsView/updateVisitedView', route)
        this.getOldData(this.$route.query.carIds, this.$route.query.workOrderType);
    },
    methods: {
        getOldData(carIds, workOrderType) {
            carWorkOrderInfo({carIds, workOrderType}).then(res => {
                let intentData = res.data[0];
                // console.log('接到', intentData)

                if (!isEmpty(intentData)) {
                    if (!isEmpty(intentData.businessDataId)) {
                        //提报过，请求详情
                        this.initData(intentData.businessDataId)
                    } else {
                        this.infoData = {...this.infoData, ...intentData}
                    }
                }
            })
        },
        initData(id) {
            this.loading = true;
            gpsRenew(id)
                .then((res) => {
                    if (res.code == 200) {
                        let data = res.data;

                        // this.infoData = data;
                        this.infoData = {...this.infoData, ...data}
                    }
                })
                .finally(() => {
                    this.loading = false;
                });
        },

        cancel() {
            this.$router.back();
            this.$store.dispatch('tagsView/delView', this.$route);
        },

        async valid() {
            let result = await this.$refs.form.valid();
            if (result) {
                this.commit()
            }
        },
        commit() {
            let that = this
            this.loading = true;

            // console.log('commit', this.infoData)
            that.infoData.contractNo = that.infoData.contractReportNumber;
            that.infoData.purchaseNo = that.infoData.procurementReportNumber;

            saveCarGpsRenew(that.infoData).then(res => {
                this.loading = false;
                this.$modal.msgSuccess("提交成功");
                this.$router.back();
                this.$store.dispatch('tagsView/delView', this.$route);
            }).finally(e => {
                this.loading = false;
            })

        },
    },
};
</script>

<style scoped lang="scss">

</style>
